package com.quan.auth.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.quan.auth.feign.UpmsUserService;
import com.quan.upms.entity.UpmsUser;

/**
 * 用户信息服务
 * 实现 Spring Security的UserDetailsService接口方法，用于身份认证
 * @author yhaoquan
 *
 */
@Service
public class SecurityUserDetailsService implements UserDetailsService {

	@Autowired
	private UpmsUserService userService;
	
	@Override
	public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
		Object userObj = this.userService.findByAccount(account);
		System.out.println(JSON.toJSON(userObj));
		UpmsUser user = JSON.parseObject(JSON.toJSONString(userObj), UpmsUser.class) ;
		System.out.println(user);
		
		if (account!=null){
            return new User(user.getAccount(), user.getPassword(), null);
        }else {
            throw  new UsernameNotFoundException("用户["+account+"]不存在");
        }
	}

}
